<template>
  <el-card>
    <el-card style="width: 90%; border: none" shadow="never">
      <div slot="header" class="clearfix">
        <span>个人信息</span>
      </div>
      <el-form label-width="80px" size="small" style="padding: 0 190px">
        <el-form-item label="头像">
          <el-upload
            class="avatar-uploader"
            :action="'http://' + serverIp + ':9090/file/upload'"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="form.avatarUrl" :src="form.avatarUrl" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>

        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="昵称">
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="电话">
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input type="textarea" v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="save">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card style="width: 90%; border: none" shadow="never">
      <div slot="header" class="clearfix">
        <span>修改密码</span>
      </div>
      <el-form label-width="120px" size="small" :model="passwordForm" :rules="rules" ref="pass" style="padding: 0 155px">
        <el-form-item label="原密码" prop="password">
          <el-input v-model="passwordForm.password" autocomplete="off" show-password></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input
            v-model="passwordForm.newPassword"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认新密码" prop="confirmPassword">
          <el-input
            v-model="passwordForm.confirmPassword"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="savePassword">确 定</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </el-card>
</template>

<script>
import { serverIp } from "../../public/config";

export default {
  name: "Person",
  data() {
    return {
      serverIp: serverIp,
      form: {},
      user: localStorage.getItem("user") ? JSON.parse(localStorage.getItem("user")) : {},
      passwordForm: {},
      rules: {
        password: [
          { required: true, message: '请输入原密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
        confirmPassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, message: '长度不少于3位', trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getUser().then((res) => {
      console.log(res);
      this.form = res;
    });
    this.passwordForm.username = this.user.username
  },
  methods: {
    async getUser() {
      return (await this.request.get("/user/username/" + this.user.username)).data;
    },
    save() {
      this.request.post("/user", this.form).then((res) => {
        if (res.code === "200") {
          this.$message.success("保存成功");

          // 触发父级更新User的方法
          this.$emit("refreshUser");

          // 更新浏览器存储的用户信息
          this.getUser().then((res) => {
            res.token = JSON.parse(localStorage.getItem("user")).token;
            localStorage.setItem("user", JSON.stringify(res));
          });
        } else {
          this.$message.error("保存失败");
        }
      });
    },
    handleAvatarSuccess(res) {
      this.form.avatarUrl = res;
    },
    savePassword() {
      this.$refs.pass.validate((valid) => {
        if (valid) {
          if (this.passwordForm.newPassword !== this.passwordForm.confirmPassword) {
            this.$message.error("2次输入的新密码不相同")
            return false
          }
          this.request.post("/user/password", this.passwordForm).then(res => {
            if (res.code === '200') {
              this.$message.success("修改成功")
              this.$store.commit("logout")
            } else {
              this.$message.error(res.msg)
            }
          })
        }
      })
    },
  },
};
</script>

<style>
.avatar-uploader {
  text-align: left;
  padding-bottom: 10px;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 16px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 138px;
  height: 138px;
  line-height: 138px;
  text-align: center;
}
.avatar {
  width: 138px;
  height: 138px;
  display: block;
}
</style>
